package com.be.beadmin.product.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.basicData.entity.User;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.design.entity.ProductDrawingInfo;
import com.be.beadmin.product.dto.input.WorkOrderInforDto;
import com.be.beadmin.product.dto.output.WorkOrderInforEx;
import com.be.beadmin.product.dto.output.WorkOrderInforExcelEx;
import com.be.beadmin.product.service.WorkOrderInforService;
import com.be.beadmin.project.dto.input.PayFileInfoDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author zjm
* @date 2023-03-30
**/
@Api(tags = {"工单管理"})
@RestController
public class WorkOrderInforController {

    @Autowired
    private  WorkOrderInforService workOrderInforService;

    @ApiOperation("获取工单by工单号 WO")
    @PostMapping(value = "/product/getWorkOrderInforByWo")
    public ResultModel getWorkOrderInforByWo(
            @ApiParam(value = "wo")@RequestParam String wo
    )  {
        return ResultModel.success(workOrderInforService.getWorkOrderInforByWo(wo)) ;
    }

    @ApiOperation("根据项目id获取项目下所有工单列表")
    @PostMapping(value = "/product/getWorkOrderInforListByInfoId")
    public ResultModel getWorkOrderInforListByInfoId(@RequestParam String infoId)  {
        return ResultModel.success(workOrderInforService.getWorkOrderInforListByInfoId(infoId)) ;
    }

    @ApiOperation("获取所有工单列表")
    @PostMapping(value = "/product/getWorkOrderInforList")
    public ResultModel getWorkOrderInforList()  {
        return ResultModel.success(workOrderInforService.getWorkOrderInforList()) ;
    }


    @ApiOperation("根据批次计划ID获取工单列表")
    @PostMapping(value = "/product/getWorkOrderInforByBatchPlanId")
    public ResultModel getWorkOrderInforByBatchPlanId(@RequestParam String batchPlanId)  {
        return ResultModel.success(workOrderInforService.getWorkOrderInforByBatchPlanId(batchPlanId)) ;
    }


    @ApiOperation("根据批次计划ID获取未排程工单列表")
    @PostMapping(value = "/product/getWorkOrderInforByBatchPlanIdAndUnscheduled")
    public ResultModel getWorkOrderInforByBatchPlanIdAndUnscheduled(@RequestParam String batchPlanId)  {
        return ResultModel.success(workOrderInforService.getWorkOrderInforByBatchPlanIdAndUnscheduled(batchPlanId)) ;
    }


    @ApiOperation("根据批次计划ID获取已排程工单列表")
    @PostMapping(value = "/product/getWorkOrderInforByBatchPlanIdAndScheduled")
    public ResultModel getWorkOrderInforByBatchPlanIdAndScheduled(@RequestParam String batchPlanId)  {
        return ResultModel.success(workOrderInforService.getWorkOrderInforByBatchPlanIdAndScheduled(batchPlanId)) ;
    }


//    @ApiOperation("根据批次Id获取所有工单列表")
//    @PostMapping(value = "/product/getWorkOrderInforListByBpId")
//    public ResultModel getWorkOrderInforListByBpId()  {
//        return ResultModel.success(workOrderInforService.getWorkOrderInforListByBpId()) ;
//    }

    @ApiOperation("获取所有工单和柜体别名对应信息")
    @PostMapping(value = "/product/getWorkOrderInforAndproductName")
    public ResultModel getWorkOrderInforAndproductName()  {
        return ResultModel.success(workOrderInforService.getWorkOrderInforAndproductName()) ;
    }

    @ApiOperation("分页查找工单信息")
    @PostMapping(value = "/product/getWorkOrderInforByPage")
    public ResultModel getWorkOrderInforByPage(HttpServletRequest request,@RequestBody PageEntity pageEntity
                                               )  {
        return ResultModel.success(workOrderInforService.getWorkOrderInforObjectByPage(request,pageEntity)) ;
    }
    @ApiOperation("分页查找工单信息-扩展版")
    @PostMapping(value = "/product/getWorkOrderInforExtendByPage")
    public ResultModel getWorkOrderInforExtendByPage(HttpServletRequest request ,@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(workOrderInforService.getWorkOrderInforExtendByPage(request,pageEntity)) ;
    }




    @ApiOperation("获取该工单的图纸信息")
    @PostMapping(value = "/product/getDrawingInfo")
    public ResultModel getDrawingInfo(@ApiParam(value = "工单ID") @RequestParam String workOrderInfoId)  {
        return ResultModel.success(workOrderInforService.getDrawingInfo(workOrderInfoId)) ;
    }

    @ApiOperation("获得工单或工序的图纸pdf、图片的预览url")
    @PostMapping("/product/getDrawingPreviewUrl")
    public ResultModel getDrawingPreviewUrl(@RequestParam String productDrawingInfoId) throws Exception {
        String previewUrl =  workOrderInforService.getDrawingPreviewUrl(productDrawingInfoId) ;
        return ResultModel.success(previewUrl);
    }

    @ApiOperation("获取该工单的生产进度")
    @PostMapping(value = "/product/getManufactInfo")
    public ResultModel getManufactInfo(@ApiParam(value = "工单ID") @RequestParam String workOrderInfoId)  {
        return ResultModel.success(workOrderInforService.getManufactInfo(workOrderInfoId)) ;
    }



    @ApiOperation("获取负责该工单的团队成员")
    @PostMapping(value = "/product/getTeamMember")
    public ResultModel getTeamMember(@RequestParam String workOrderInfoId)  {
        List<User> userList = workOrderInforService.getTeamMember(workOrderInfoId);
        return ResultModel.success(userList) ;
    }

    @ApiOperation("获取负责该工单的生产经理")
    @PostMapping(value = "/product/getTeamManufactManager")
    public ResultModel getTeamManufactManager(@RequestParam String workOrderInfoId)  {
        User manager = workOrderInforService.getTeamManufactManager(workOrderInfoId);
        return ResultModel.success(manager) ;
    }

    @ApiOperation("获取负责该工单的项目经理")
    @PostMapping(value = "/product/getTeamManager")
    public ResultModel getTeamManager(@RequestParam String workOrderInfoId)  {
        User manager = workOrderInforService.getTeamManager(workOrderInfoId);
        return ResultModel.success(manager) ;   
    }


    @PostMapping("/product/addWorkOrderInfor")
    public ResultModel addWorkOrderInfor(@RequestBody WorkOrderInforDto workOrderInforDto){
        workOrderInforService.addWorkOrderInfor(workOrderInforDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/product/delWorkOrderInfor")
    public ResultModel delWorkOrderInfor(@RequestBody WorkOrderInforDto workOrderInforDto){
        workOrderInforService.delWorkOrderInfor(workOrderInforDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/product/updateWorkOrderInfor")
    public ResultModel updateWorkOrderInfor(@RequestBody WorkOrderInforDto workOrderInforDto){
        workOrderInforService.updateWorkOrderInfor(workOrderInforDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/product/exportWorkOrderInfor")
    public void exportWorkOrderInfor(HttpServletRequest request,HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<WorkOrderInforEx> workOrderInforPage = workOrderInforService.getWorkOrderInforByPage(request,pageEntity);
        List<WorkOrderInforEx> workOrderInforExes = workOrderInforPage.getContent();
        List<WorkOrderInforExcelEx> workOrderInforExcelExes=workOrderInforExes.stream().map(item->{
            WorkOrderInforExcelEx  excel=new WorkOrderInforExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("WorkOrderInfor接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), WorkOrderInforExcelEx.class).sheet("sheet1").doWrite(workOrderInforExcelExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), WorkOrderInforExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(workOrderInforExcelExes);
    }

    @ApiOperation("获取工单进度")
    @PostMapping("/product/getWorkOrderInforProgress")
    public ResultModel getWorkOrderInforProgress(){
        ArrayList<Integer> workOrderInforProgress = new ArrayList<>();
        workOrderInforProgress = workOrderInforService.getWorkOrderInforProgress();
        return ResultModel.success(workOrderInforProgress);
    }

    @ApiOperation("下拉框中获取符合条件工单编号")
    @PostMapping("/product/getWos")
    public ResultModel getWos(@RequestParam(defaultValue = "") String s){
        return ResultModel.success(workOrderInforService.getWos(s));
    }

    @ApiOperation("获取本月/年工单结束数量")
    @PostMapping("getWorkOrderInforProgressByDate")
    public ResultModel getWorkOrderInforProgressByDate() {

        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        Integer month = zonedDateTime.getMonth().getValue();
        Integer year  = zonedDateTime.getYear();

        return ResultModel.success(workOrderInforService.countYearMonthWorkOrderInforByDate(month,year));
    }

    @ApiOperation("根据项目编码获取工单编号列表")
    @PostMapping("/product/getWoListByProjectCode")
    public ResultModel getWoListByProjectCode(@RequestParam String projectCode){
        return ResultModel.success(workOrderInforService.getWoListByProjectCode(projectCode));
    }
    @ApiOperation("Test超期预警查询")
    @PostMapping("/product/getWorkOrderInforExtendWarnByPage")
    public ResultModel getWorkOrderInforExtendWarnByPage(){
        return ResultModel.success(workOrderInforService.getWorkOrderInforExtendWarnByPage());
    }
}